/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.io;

import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

/**
 * wrapper around a session.
 */
public class HttpSessionWrapper
    implements HttpSession
{

  /**
   * the wrapped session.
   */
  private HttpSession session;



  /**
   * constructor.
   * 
   * @param session
   *          the wrapped session
   */
  public HttpSessionWrapper(final HttpSession session)
  {
    this.session = session;
  }



  /**
   * Returns the object bound with the specified name in this session, or
   * <code>null</code> if no object is bound under the name.
   * 
   * @param name
   *          a string specifying the name of the object
   * @return the object with the specified name
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String)
   */
  public Object getAttribute(final String name)
      throws IllegalStateException
  {
    return session.getAttribute(name);
  }



  /**
   * Returns an <code>Enumeration</code> of <code>String</code> objects
   * containing the names of all the objects bound to this session.
   * 
   * @return an <code>Enumeration</code> of <code>String</code> objects
   *         specifying the names of all the objects bound to this session
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @see javax.servlet.http.HttpSession#getAttributeNames()
   */
  @SuppressWarnings("unchecked")
  public Enumeration<String> getAttributeNames()
      throws IllegalStateException
  {
    return session.getAttributeNames();
  }



  /**
   * Returns the time when this session was created, measured in milliseconds
   * since midnight January 1, 1970 GMT.
   * 
   * @return a <code>long</code> specifying when this session was created,
   *         expressed in milliseconds since 1/1/1970 GMT
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @see javax.servlet.http.HttpSession#getCreationTime()
   */
  public long getCreationTime()
      throws IllegalStateException
  {
    return session.getCreationTime();
  }



  /**
   * Returns a string containing the unique identifier assigned to this session.
   * The identifier is assigned by the servlet container and is implementation
   * dependent.
   * 
   * @return a string specifying the identifier assigned to this session
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @see javax.servlet.http.HttpSession#getId()
   */
  public String getId()
      throws IllegalStateException
  {
    return session.getId();
  }



  /**
   * Returns the last time the client sent a request associated with this
   * session, as the number of milliseconds since midnight January 1, 1970 GMT,
   * and marked by the time the container received the request.
   * <p>
   * Actions that your application takes, such as getting or setting a value
   * associated with the session, do not affect the access time.
   * 
   * @return a <code>long</code> representing the last time the client sent a
   *         request associated with this session, expressed in milliseconds
   *         since 1/1/1970 GMT
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @see javax.servlet.http.HttpSession#getLastAccessedTime()
   */
  public long getLastAccessedTime()
      throws IllegalStateException
  {
    return session.getLastAccessedTime();
  }



  /**
   * Returns the maximum time interval, in seconds, that the servlet container
   * will keep this session open between client accesses. After this interval,
   * the servlet container will invalidate the session. The maximum time
   * interval can be set with the <code>setMaxInactiveInterval</code> method.
   * A negative time indicates the session should never timeout.
   * 
   * @return an integer specifying the number of seconds this session remains
   *         open between client requests
   * @see #setMaxInactiveInterval
   * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
   */
  public int getMaxInactiveInterval()
  {
    return session.getMaxInactiveInterval();
  }



  /**
   * Returns the ServletContext to which this session belongs.
   * 
   * @return The ServletContext object for the web application
   * @since Servlet 2.3
   * @see javax.servlet.http.HttpSession#getServletContext()
   */
  public ServletContext getServletContext()
  {
    return session.getServletContext();
  }



  /**
   * getter for the session property.
   * 
   * @return the session
   */
  public HttpSession getSession()
  {
    return session;
  }



  /**
   * As of Version 2.1, this method is deprecated and has no replacement. It
   * will be removed in a future version of the Java Servlet API.
   * 
   * @return null
   * @deprecated
   * @see javax.servlet.http.HttpSession#getSessionContext()
   */
  @Deprecated
  public javax.servlet.http.HttpSessionContext getSessionContext()
  {
    return session.getSessionContext();
  }



  /**
   * As of Version 2.2, this method is replaced by {@link #getAttribute}.
   * 
   * @param name
   *          a string specifying the name of the object
   * @return the object with the specified name
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @deprecated
   * @see javax.servlet.http.HttpSession#getValue(java.lang.String)
   */
  @Deprecated
  public Object getValue(final String name)
      throws IllegalStateException
  {
    return session.getValue(name);
  }



  /**
   * As of Version 2.2, this method is replaced by {@link #getAttributeNames}.
   * 
   * @return an array of <code>String</code> objects specifying the names of
   *         all the objects bound to this session
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @deprecated
   * @see javax.servlet.http.HttpSession#getValueNames()
   */
  @Deprecated
  public String[] getValueNames()
      throws IllegalStateException
  {
    return session.getValueNames();
  }



  /**
   * Invalidates this session then unbinds any objects bound to it.
   * 
   * @exception IllegalStateException
   *              if this method is called on an already invalidated session
   * @see javax.servlet.http.HttpSession#invalidate()
   */
  public void invalidate()
      throws IllegalStateException
  {
    session.invalidate();
  }



  /**
   * Returns <code>true</code> if the client does not yet know about the
   * session or if the client chooses not to join the session. For example, if
   * the server used only cookie-based sessions, and the client had disabled the
   * use of cookies, then a session would be new on each request.
   * 
   * @return <code>true</code> if the server has created a session, but the
   *         client has not yet joined
   * @exception IllegalStateException
   *              if this method is called on an already invalidated session
   * @see javax.servlet.http.HttpSession#isNew()
   */
  public boolean isNew()
      throws IllegalStateException
  {
    return session.isNew();
  }



  /**
   * As of Version 2.2, this method is replaced by {@link #setAttribute}.
   * 
   * @param name
   *          the name to which the object is bound; cannot be null
   * @param value
   *          the object to be bound; cannot be null
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @deprecated
   * @see javax.servlet.http.HttpSession#putValue(java.lang.String,
   *      java.lang.Object)
   */
  @Deprecated
  public void putValue(final String name, final Object value)
      throws IllegalStateException
  {
    session.putValue(name, value);
  }



  /**
   * Removes the object bound with the specified name from this session. If the
   * session does not have an object bound with the specified name, this method
   * does nothing.
   * <p>
   * After this method executes, and if the object implements
   * <code>HttpSessionBindingListener</code>, the container calls
   * <code>HttpSessionBindingListener.valueUnbound</code>. The container then
   * notifies any <code>HttpSessionAttributeListener</code>s in the web
   * application.
   * 
   * @param name
   *          the name of the object to remove from this session
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
   */
  public void removeAttribute(final String name)
      throws IllegalStateException
  {
    session.removeAttribute(name);
  }



  /**
   * As of Version 2.2, this method is replaced by {@link #removeAttribute}.
   * 
   * @param name
   *          the name of the object to remove from this session
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @deprecated
   * @see javax.servlet.http.HttpSession#removeValue(java.lang.String)
   */
  @Deprecated
  public void removeValue(final String name)
      throws IllegalStateException
  {
    session.removeValue(name);
  }



  /**
   * Binds an object to this session, using the name specified. If an object of
   * the same name is already bound to the session, the object is replaced.
   * <p>
   * After this method executes, and if the new object implements
   * <code>HttpSessionBindingListener</code>, the container calls
   * <code>HttpSessionBindingListener.valueBound</code>. The container then
   * notifies any <code>HttpSessionAttributeListener</code>s in the web
   * application.
   * <p>
   * If an object was already bound to this session of this name that implements
   * <code>HttpSessionBindingListener</code>, its
   * <code>HttpSessionBindingListener.valueUnbound</code> method is called.
   * <p>
   * If the value passed in is null, this has the same effect as calling
   * <code>removeAttribute()</code>.
   * 
   * @param name
   *          the name to which the object is bound; cannot be null
   * @param value
   *          the object to be bound
   * @exception IllegalStateException
   *              if this method is called on an invalidated session
   * @see javax.servlet.http.HttpSession#setAttribute(java.lang.String,
   *      java.lang.Object)
   */
  public void setAttribute(final String name, final Object value)
      throws IllegalStateException
  {
    session.setAttribute(name, value);
  }



  /**
   * Specifies the time, in seconds, between client requests before the servlet
   * container will invalidate this session. A negative time indicates the
   * session should never timeout.
   * 
   * @param interval
   *          An integer specifying the number of seconds
   * @see javax.servlet.http.HttpSession#setMaxInactiveInterval(int)
   */
  public void setMaxInactiveInterval(final int interval)
  {
    session.setMaxInactiveInterval(interval);
  }



  /**
   * setter for the session property.
   * 
   * @param session
   *          the session to set
   */
  public void setSession(final HttpSession session)
  {
    this.session = session;
  }

}
